﻿// =================================================================== 
// 数据访问层(GDIT.DAL)
//====================================================================
// 文件：DDepartment.cs
// 项目名称：学院网站
// 创建时间：2012-08-08
// 负责人：ChenZhiGang
//引用DevExpress.Data.Filtering，DevExpress.Xpo程序集
// 程序集介绍：部门信息数据访问层
// ===================================================================
using GDIT.Model;
using DevExpress.Data.Filtering;
using DevExpress.Xpo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDIT.Pattern;

namespace GDIT.DAL
{
    public class DDepartment : IDepartment
    {

        private XpoHelper.Test<Mtb_Department> userOperation = null;

        /// <summary>
        /// 增加部门
        /// </summary>
        /// <param name="department">需要新增的部门对象</param>
        /// <returns>1: 更新成功 0：更新失败</returns>
        public int InsertDepartment(Mtb_Department department)
        {
            return XpoHelper.SaveAdded(department);
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="department">需要删除的部门对象</param>
        /// <returns>1: 更新成功 0：更新失败</returns>
        public int DeleteDepartment(Mtb_Department department)
        {
            userOperation = new XpoHelper.Test<Mtb_Department>();
            return userOperation.Delete(department.OID);
        }

        /// <summary>
        /// 更新部门
        /// </summary>
        /// <param name="department">需要更新的部门对象</param>
        /// <returns>1: 更新成功 0：更新失败</returns>
        public int UpdateDepartment(Mtb_Department department)
        {
            userOperation = new XpoHelper.Test<Mtb_Department>();
            XPCollection xp = userOperation.Operation("OID", department.OID);
            foreach (Mtb_Department b in xp)
            {
                b.DepartmentName = department.DepartmentName;
                return XpoHelper.SaveModified(b);
            }
            return 0;
        }

        /// <summary>
        /// 查询所有的部门
        /// </summary>
        /// <returns>所有部门的列表</returns>
        public XPCollection SelectAllDepartment()
        {
            userOperation = new XpoHelper.Test<Mtb_Department>();
            return userOperation.Operation();
        }

        /// <summary>
        /// 查询单一的部门对象
        /// </summary>
        /// <param name="department">需要查询的单一部门对象</param>
        /// <returns>单一部门对象</returns>
        public Mtb_Department SelectDepartmentByDepart(Mtb_Department department)
        {
            Mtb_Department result = null;
            userOperation = new XpoHelper.Test<Mtb_Department>();
            XPCollection xp = userOperation.Operation("DepartmentName", department.DepartmentName);
            foreach (Mtb_Department d in xp)
            {
                result = d;
            }
            return result;
        }
    }
}
